/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EirinPanel.java
 *
 * Created on Oct 26, 2010, 11:38:45 PM
 */

package lmclogistics;

import components.Dates;
import components.Text;
import components.TextArea;
import components.Image;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import sql.Sql;

/**
 *
 * @author Sonia Tan
 */
public class EirinPanel extends javax.swing.JPanel {

    /** Creates new form EirinPanel */

    private static TextArea inCodeField = new TextArea();
    private static Dates date = new Dates();
    private static Times time = new Times();
    private static TextArea eirOutField = new TextArea();
    private static TextArea cwsNoField = new TextArea();
    private static Text customerField = new Text();
    private static TextArea containerNoField = new TextArea();
    private static JComboBox truckingField = new JComboBox();
    private static TextArea plateNoField = new TextArea();
    private static TextArea licenseNoField = new TextArea();
    private static TextArea takenByField = new TextArea();
    private static JComboBox consigneeField = new JComboBox();
    private static JTextArea remarksField = new JTextArea();
    private static Text chassisField = new Text();
    private Image bg;
    private static int eirout;


    public EirinPanel() {
        initComponents();
        setOpaque(false);
        setLocation(getCenterX(507), getCenterY(460));
        setSize(507, 460);
        setVisible(true);
        setBg();
        initText();
        initInput();
        initLinks();
    }

    public void popCombo()
    {
        String consQuery = "SELECT consFName, consLName FROM consignee WHERE deleted=0";
        String trkQuery = "SELECT trkName FROM trucking WHERE deleted=0";

        try {
                Connection connection = Sql.getSqlConnection();
                Statement stmt = connection.createStatement();

                ResultSet rs = stmt.executeQuery(consQuery);
                consigneeField.insertItemAt("<Consignee>", 0);
                consigneeField.setSelectedIndex(0);
                int i = 1;
                while(rs.next())
                {
                    consigneeField.insertItemAt(rs.getString(1)+" "+rs.getString(2), i);
                    i++;
                }

                rs = stmt.executeQuery(trkQuery);
                truckingField.insertItemAt("<Trucking>", 0);
                truckingField.setSelectedIndex(0);
                i=1;
                while(rs.next())
                {
                    truckingField.insertItemAt(rs.getString(1),i);
                    i++;
                }

                Sql.closeSqlConnection(connection);
        } catch (Exception ex) {
                Logger.getLogger(AddUser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    public void initText(){

         Text code = new Text();
         Text date = new Text();
         Text time = new Text();
         Text eirOut = new Text();
         Text cwsNo = new Text();
         Text customer = new Text();
         Text containerNo = new Text();
         Text trucking = new Text();
         Text plateNo = new Text();
         Text licenseNo = new Text();
         Text takenBy = new Text();
         Text consignee = new Text();
         Text remarks = new Text();
         Text chassis = new Text();
         Image bg;

        code.inText("EIR In Code:",15,15);  
        eirOut.inText("EIR OUT:", 205, 15);
        cwsNo.inText("CWS No.:", 355, 15);
        date.inText("Date:",15, 50);
        time.inText("Time:", 310, 50);
        chassis.inText("Chassis:", 15, 75);
        customer.inText("Customer:", 15, 100);
        containerNo.inText("Container No.:",15,125);
        trucking.inText("Trucking:",15, 150);
        plateNo.inText("Plate No.:", 15, 175);
        licenseNo.inText("License No.:", 300, 200);
        takenBy.inText("Taken By:", 15, 200);
        consignee.inText("Consignee:", 15, 225);
        remarks.inText("Remarks:", 15, 275);

        add(code,0);
        add(date,0);
        add(time,0);
        add(eirOut,0);
        add(cwsNo,0);
        add(chassis,0);
        add(customer,0);
        add(containerNo,0);
        add(trucking,0);
        add(plateNo,0);
        add(takenBy,0);
        add(licenseNo,0);
        add(consignee,0);
        add(remarks,0);
    }

    public final void setBg(){
        bg = new Image("panel.png",507,460);

        add(bg,-1);
        
    }

    public int getCenterX(int x){

        int a = 919/2 - x/2;


        return a;
    }

    public int getCenterY(int x){

        int a = 577/2 - x/2;


        return a;
    }

    public void initInput(){           
      inCodeField.initText(90, 25, 105, 12);
      eirOutField.initText(80,25,270,12);
      cwsNoField.initText(80, 25, 420, 12);
      date.initDates(350,25,68,48);
      time.initTimes(350, 25, 350, 48);      
      containerNoField.initText(150, 25, 120, 122);
      plateNoField.initText(100, 25, 85, 172);
      takenByField.initText(175, 25, 85, 197);
      licenseNoField.initText(100, 25, 390, 197);
      remarksField.setSize(475, 100);
      remarksField.setLocation(15, 295);
      remarksField.setText("");

      consigneeField.setLocation(100, 222);
      consigneeField.setSize(200,25);

      truckingField.setLocation(82, 147);
      truckingField.setSize(200,25);

      eirOutField.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae)
          {
              eirout = Integer.parseInt(eirOutField.getText());
              String check = "SELECT rentStatus FROM rental WHERE outCode="+eirout;

              try{
                    Connection connection = Sql.getSqlConnection();
                    Statement stmt = connection.createStatement();
                    ResultSet rs = stmt.executeQuery(check);

                    if(rs.next())
                    {
                        if(rs.getString(1).equals("out"))
                        {
                            String query = "SELECT r.chsCode, c.custName, co.consFName, co.consLName, t.trkName"
                                  +" FROM rental r, customer c, consignee co, trucking t WHERE "
                                  +"outCode="+eirout+" AND r.custID=c.custCode"
                                  +" AND r.outConsignee=co.consCode AND r.outTrkCode=t.trkCode";

                              try{
                                    rs = stmt.executeQuery(query);

                                    if(rs.next())
                                    {
                                        chassisField.inText(rs.getString(1), 80,75);
                                        customerField.inText(rs.getString(2), 97,100);

                                        for(int i=consigneeField.getItemCount()-1;i>0;i--)
                                        {
                                            if(consigneeField.getItemAt(i).equals(rs.getString(3)+" "+rs.getString(4)))
                                                consigneeField.setSelectedIndex(i);
                                        }

                                        for(int i=truckingField.getItemCount()-1;i>0;i--)
                                        {
                                            if(truckingField.getItemAt(i).equals(rs.getString(5)))
                                                truckingField.setSelectedIndex(i);
                                        }
                                    }                                    
                              } catch(Exception ex)
                              {
                                  Logger.getLogger(EirinPanel.class.getName()).log(Level.SEVERE, null, ex);
                              }
                        }
                        else
                        {
                            System.out.println("@.@");
                            //PARA HINDI MAGKA OVERWRITE @.@
                        }
                    }                    

                    Sql.closeSqlConnection(connection);
              } catch(Exception ex)
              {
                    Logger.getLogger(EirinPanel.class.getName()).log(Level.SEVERE, null, ex);
              }              
          }
      });

      popCombo();

      add(inCodeField,0);
      add(cwsNoField, 0);
      add(eirOutField,0);
      add(date,0);
      add(time,0);
      add(chassisField, 0);
      add(customerField, 0);
      add(containerNoField,0);
      add(truckingField,0);
      add(plateNoField,0);
      add(licenseNoField,0);
      add(takenByField,0);
      add(consigneeField,0);
      add(remarksField, 0);
    }

    public static void addEirIn()
    {
        String cons[] = (consigneeField.getSelectedItem().toString()).split(" ");

        String chsQuery = "SELECT chsPin FROM chassis WHERE chsSrlNo="+Integer.parseInt(chassisField.getText());
        String custQuery = "SELECT custCode FROM customer WHERE custName='"+customerField.getText()+"'";
        String trkQuery = "SELECT trkCode FROM trucking WHERE trkName='"+truckingField.getSelectedItem().toString()+"'";
        String consQuery = "SELECT consCode FROM consignee WHERE consFName='"+cons[0]+"' AND consLName='"+cons[1]+"'";

        int chassis=0;
        int customer=0;
        int trucking=0;
        int consignee=0;

        try{
            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();

            ResultSet rs = stmt.executeQuery(chsQuery);
            if(rs.next())
                chassis = Integer.parseInt(rs.getString(1));

            rs = stmt.executeQuery(custQuery);
            if(rs.next())
                customer = Integer.parseInt(rs.getString(1));

            rs = stmt.executeQuery(trkQuery);
            if(rs.next())
                trucking = Integer.parseInt(rs.getString(1));

            rs = stmt.executeQuery(consQuery);
            if(rs.next())
                consignee = Integer.parseInt(rs.getString(1));

            Sql.closeSqlConnection(connection);
        } catch(Exception ex){
            Logger.getLogger(EiroutPanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        String query = "UPDATE rental SET rentStatus='in', inCode='"+inCodeField.getText()+"', "
                + "inCwsNo='"+cwsNoField.getText()+"', inDate='"+date.getDate()+"', inTime='"+time.getTime()
                +"', inContainer='"+containerNoField.getText()+"', inTrkCode="+trucking+","
                + "inPltNo='"+plateNoField.getText()+"', inTakenBy='"+takenByField.getText()+"',"
                + "inLicNo='"+licenseNoField.getText()+"', inConsignee="+consignee+", inRemarks='"+remarksField.getText()+"'"
                + "WHERE outCode="+eirout;

        try{
            Connection connection = Sql.getSqlConnection();
            Statement stmt = connection.createStatement();
            stmt.executeUpdate(query);

            Sql.closeSqlConnection(connection);
        } catch(Exception ex){
            Logger.getLogger(EiroutPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void initLinks()
    {
        Text truckingLink = new Text();
        Text consigneeLink = new Text();

        Font font = new Font("Arial", Font.PLAIN, 11);
        truckingLink.setFont(font);
        consigneeLink.setFont(font);

        truckingLink.inText("click here to add new trucking",295,150);
        consigneeLink.inText("click here to add new consignee",305,225);
        truckingLink.setSize(200,25);
        consigneeLink.setSize(200,25);

        truckingLink.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent me) {
                AddTrucking at = new AddTrucking();
            }

            public void mousePressed(MouseEvent me) {
            }

            public void mouseReleased(MouseEvent me) {
            }

            public void mouseEntered(MouseEvent me) {
            }

            public void mouseExited(MouseEvent me) {
            }
        });

        consigneeLink.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent me) {
                AddConsignee ac = new AddConsignee();
            }

            public void mousePressed(MouseEvent me) {
            }

            public void mouseReleased(MouseEvent me) {
            }

            public void mouseEntered(MouseEvent me) {
            }

            public void mouseExited(MouseEvent me) {
            }
        });
        
        add(truckingLink,0);
        add(consigneeLink,0);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
